package com.lanou.zhihu.service.impl;

import com.lanou.zhihu.entity.Answer;
import com.lanou.zhihu.mapper.AnswerMapper;
import com.lanou.zhihu.service.AnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by QuChao on 2018/10/26.
 */
@Service
public class AnswerServiceImpl implements AnswerService {
    @Autowired
    private AnswerMapper answerMapper;

    @Override
    public boolean addAnswer(Answer answer) {
        Integer result = answerMapper.insert(answer);
        if (result == 1) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean delete(Integer uid, Integer aid) {
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("aid", aid);
        Answer answer = answerMapper.findByUidAid(map);
        if (answer != null) {
            Integer result = answerMapper.deleteById(aid);
            if (result == 1) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    @Override
    public List<Answer> findByQid(Integer qid) {
        List<Answer> answerList = answerMapper.findByQid(qid);
        return answerList;
    }
}
